export function useObjParams(url: string): {} {
  let params: any = {};
  if (url.indexOf('?') !== -1) {
    let index = url.indexOf('?') + 1;
    let str = url.substr(index);
    let ary = str.split('&');
    for (let i = 0; i < ary.length; i++) {
      params[ary[i].split('=')[0]] = ary[i].split('=')[1];
    }
  }
  return params;
};

export const useCopyToClipboard = (str: string) => {
  let container = document.createElement('input');
  container.setAttribute('type', 'text');
  container.value = str;
  container.setAttribute('style', 'opacity: 0');
  document.body.appendChild(container);
  container.select();
  if (document.execCommand('copy')) {
    document.execCommand('copy');
  }
  document.body.removeChild(container);
};

export const useDeepClone = (source: any, recursion: boolean) => {
  if (recursion) {
    if (source === null || source === undefined || source === 0 || !isNaN(source)) {
      return source;
    }
    if (source.constructor === String || source.constructor === Boolean) {
      return source;
    }
    if (source.constructor === Date) {
      return new Date(source);
    }
    let target: any = source.constructor === Array ? [] : {};
    if (source.constructor === Object) {
      for (let key in source) {
        if (source.hasOwnProperty(key)) {
          if (source[key].constructor === Object) {
            target[key] = useDeepClone(source[key], false);
          } else {
            target[key] = source[key];
          }
        }
      }
    } else if (source.constructor === Array) {
      for (let i = 0; i < source.length; i++) {
        target.push(useDeepClone(source[i], false));
      }
    }
    return target;
  } else {
    if (source.constructor !== Object) {
      return source;
    }
    return JSON.parse(JSON.stringify(source));
  }
};

export const useMobile = (navigator: any) => {
  if (navigator?.userAgent) {
    return {
      isMobile: /iPhone|iPad|iPod|Android|HarmonyOS/i.test(navigator.userAgent),
      isIOS: /iPhone|iPad|iPod/i.test(navigator.userAgent),
      isAndroid: /Android/i.test(navigator.userAgent),
      isHarmonyOS: /HarmonyOS/i.test(navigator.userAgent),
    }
  } else {
    throw new Error(`Not the right Navigator Object, Please check your params!`)
  }
};

export const useFmtSize = (size: number | undefined, digit: number | undefined): string => {
  let num = 0,
    unit = 'B';
  if (size === undefined) {
    size = 0;
  }
  if (size >= 0 && size < 1024) {
    num = size;
    unit = 'B';
  } else if (size >= 1024 && size < 1024 * 1024) {
    num = size / 1024;
    unit = 'KB';
  } else if (size >= 1024 * 1024 && size < 1024 * 1024 * 1024) {
    num = size / (1024 * 1024);
    unit = 'MB';
  } else if (size >= 1024 * 1024 * 1024) {
    num = size / (1024 * 1024 * 1024);
    unit = 'G';
  }
  return `${num.toFixed(!digit ? 2 : digit)}${unit}`;
};

export const useGenerateUUID = () => {
  const timestamp = new Date().getTime();
  const randomPart = Math.random().toString(16).substring(2, 18);
  const uuid = timestamp.toString(16) + randomPart;
  return uuid;
};

type TTimeout = any;
export const useDebounce = (fn: (arg: any) => void, wait: number) => {
  let timeout: null | TTimeout = null;
  return (input: any) => {
    input.persist();
    if (timeout !== null) clearTimeout(timeout);
    timeout = setTimeout(fn, wait, input);
  };
};
